<?php

//CONECTAR A BASE DE DATOS - Por ahora me conecto a localhost
$conexion = pg_connect("host=localhost port=5432 dbname=db6 user=grupo6 password=gruposeis");

//Consulta Productos mas Vendidos
$query = "SELECT p.codigo, p.modelo, p.descripcion, COALESCE(cantidad, 0) as cantidad
    FROM
        (SELECT codigo, modelo, descripcion, COUNT(*) as cantidad
        FROM producto NATURAL JOIN cliente_producto
        GROUP BY codigo) as q
        RIGHT OUTER JOIN
        producto as p
        ON q.codigo = p.codigo
    ORDER BY cantidad DESC;";

$result = pg_query($conexion,$query);
//Crear documento mediante DOM
$version = '1.0';
$encoding = 'utf-8';
$doc = new DOMDocument($version, $encoding);
$channel = $doc->appendChild($doc->createElement('channel'));
$title=$channel->appendChild($doc->createElement('title','Productos mas Vendidos'));
while($row = pg_fetch_assoc($result))
{
    $item = $channel->appendChild($doc->createElement('item'));
    
    foreach($row as $key=>$value)
    {
        $nodo = $item->appendChild($doc->createElement($key, $value));
    }
}
$doc->formatOutput = true;
$doc->save('consulta3.xml');
//incluir el xml! 
header('Content-type: text/xml');
//MODIFICAR ESTO
$file = file_get_contents('http://localhost/PruebaRSS/consulta3.xml');
echo $file;
?>
